package com.dianping.cat.home.jar.transform;

import com.dianping.cat.home.jar.IVisitor;
import com.dianping.cat.home.jar.entity.Domain;
import com.dianping.cat.home.jar.entity.Jar;
import com.dianping.cat.home.jar.entity.JarReport;
import com.dianping.cat.home.jar.entity.Machine;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;


public class DefaultNativeBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private DataOutputStream m_out;
  
/*  21 */   public DefaultNativeBuilder(OutputStream out) { this(out, null); }

  
  public DefaultNativeBuilder(OutputStream out, IVisitor visitor) {
    this.m_out = new DataOutputStream(out);
/*  26 */     this.m_visitor = (visitor == null) ? this : visitor;
  }
  
  public static byte[] build(JarReport jarReport) {
/*  30 */     ByteArrayOutputStream out = new ByteArrayOutputStream(8192);

    build(jarReport, out);
/*  33 */     return out.toByteArray();
  }

  
/*  37 */   public static void build(JarReport jarReport, OutputStream out) { jarReport.accept(new DefaultNativeBuilder(out)); }


  
  public void visitDomain(Domain domain) {
/*  42 */     if (domain.getId() != null) {
/*  43 */       writeTag(1, 1);
/*  44 */       writeString(domain.getId());
    }
    
/*  47 */     if (!domain.getMachines().isEmpty()) {
/*  48 */       writeTag(33, 2);
/*  49 */       writeInt(domain.getMachines().size());

/*  51 */       for (Machine machine : domain.getMachines().values()) {
/*  52 */         machine.accept(this.m_visitor);
      }
    }
    
/*  56 */     writeTag(63, 3);
  }

  
  public void visitJar(Jar jar) {
/*  61 */     if (jar.getId() != null) {
/*  62 */       writeTag(1, 1);
/*  63 */       writeString(jar.getId());
    }
    
/*  66 */     if (jar.getVersion() != null) {
/*  67 */       writeTag(2, 1);
/*  68 */       writeString(jar.getVersion());
    }
    
/*  71 */     writeTag(63, 3);
  }

  
  public void visitJarReport(JarReport jarReport) {
/*  76 */     writeTag(63, 0);

    if (jarReport.getDomain() != null) {
/*  79 */       writeTag(1, 1);
/*  80 */       writeString(jarReport.getDomain());
    }
    
/*  83 */     if (jarReport.getStartTime() != null) {
/*  84 */       writeTag(2, 1);
/*  85 */       writeDate(jarReport.getStartTime());
    }
    
/*  88 */     if (jarReport.getEndTime() != null) {
/*  89 */       writeTag(3, 1);
/*  90 */       writeDate(jarReport.getEndTime());
    }
    
/*  93 */     if (!jarReport.getDomains().isEmpty()) {
/*  94 */       writeTag(33, 2);
      writeInt(jarReport.getDomains().size());

/*  97 */       for (Domain domain : jarReport.getDomains().values()) {
/*  98 */         domain.accept(this.m_visitor);
      }
    }
    
/* 102 */     writeTag(63, 3);
  }

  
  public void visitMachine(Machine machine) {
/* 107 */     if (machine.getId() != null) {
/* 108 */       writeTag(1, 1);
/* 109 */       writeString(machine.getId());
    }
    
/* 112 */     if (!machine.getJars().isEmpty()) {
/* 113 */       writeTag(33, 2);
/* 114 */       writeInt(machine.getJars().size());

/* 116 */       for (Jar jar : machine.getJars()) {
/* 117 */         jar.accept(this.m_visitor);
      }
    }
    
/* 121 */     writeTag(63, 3);
  }
  
  private void writeDate(Date value) {
    try {
/* 126 */       writeVarint(value.getTime());
/* 127 */     } catch (IOException e) {
/* 128 */       throw new RuntimeException(e);
    }
  }
  
  private void writeInt(int value) {
    try {
/* 134 */       writeVarint(value & 0xFFFFFFFFL);
/* 135 */     } catch (IOException e) {
/* 136 */       throw new RuntimeException(e);
    }
  }
  
  private void writeString(String value) {
    try {
/* 142 */       this.m_out.writeUTF(value);
/* 143 */     } catch (IOException e) {
/* 144 */       throw new RuntimeException(e);
    }
  }
  
  private void writeTag(int field, int type) {
    try {
/* 150 */       this.m_out.writeByte((field << 2) + type);
/* 151 */     } catch (IOException e) {
/* 152 */       throw new RuntimeException(e);
    }
  }
  
  protected void writeVarint(long value) throws IOException {
    while (true) {
/* 158 */       if ((value & 0xFFFFFFFFFFFFFF80L) == 0L) {
/* 159 */         this.m_out.writeByte((byte)(int)value);
        return;
      }
/* 162 */       this.m_out.writeByte((byte)(int)value & 0x7F | 0x80);
/* 163 */       value >>>= 7;
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/classes/!/com/dianping/cat/home/jar/transform/DefaultNativeBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */